<%@ page language="java" contentType="text/html;charset=UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <%@include file="/assets/publicjsp.jsp"%>
    <title>查看用户</title>
</head>
<body>
<%@include file="../assets/header.jsp"%>

<div class="d-flex align-items-stretch">
    <!-- Sidebar Navigation-->
    <%@include file="../assets/side.jsp"%>
    <!-- Sidebar Navigation end-->
    <div class="page-content">
        <div class="page-header">
            <div class="container-fluid">
                <h2 class="h5 no-margin-bottom">用户列表</h2>
            </div>
        </div>
        <section class="no-padding-bottom">
            <div class="title">
                <form class="form-inline" action="${pageContext.request.contextPath}/user/search" type="post">
                    <div class="form-group">
                        <label for="inlineFormInput" class="sr-only">Name</label>
                        <input id="inlineFormInput" value="${realName}" name="realName" type="text" placeholder="按名字查找" class="mr-sm-3 form-control">
                    </div>
                    <div class="form-group">
                        <input type="submit" value="查询" class="btn btn-primary">
                    </div>
                </form>

            </div>
            <div class="table-responsive">
                <table class="table">
                    <thead>
                    <tr>
                        <th>ID</th>
                        <th>姓名</th>
                        <th>性别</th>
                        <th>年龄</th>
                        <th>简介</th>
                        <th>操作</th>
                        <th>加关注</th>
                    </tr>
                    </thead>
                    <tbody>
                    <c:forEach var="user" items="${pageData.data}">
                        <tr>
                            <td scope="row">${user.id}</td>
                            <c:choose>
                                <c:when test="${user.realName != null}">
                                    <td>${user.realName}</td>
                                </c:when>
                                <c:otherwise>
                                    <td>${user.username}</td>
                                </c:otherwise>
                            </c:choose>
                            <td>
                                <c:choose>
                                    <c:when test="${user.gender == '1'}">
                                        男
                                    </c:when>
                                    <c:when test="${user.gender == '0'}">
                                        女
                                    </c:when>
                                    <c:when test="${user.gender == '' || user.gender == null}">
                                        未知
                                    </c:when>
                                </c:choose>
                            </td>
                            <td>${user.age}</td>
                            <td>${user.desc}</td>
                            <td>
                                <input type="button" data-id="${user.id}" value="详细信息" class="btn-xs btn-primary userDetail">
                            </td>
                            <td>
                                <input type="checkbox" ${focus.contains(user.id)? 'checked':''} class="checkbox-template"
                                       onclick="addFocus(event,${user.id})">
                            </td>
                        </tr>
                    </c:forEach>
                    </tbody>
                </table>

                <nav class="text-center" aria-label="Page navigation">
                    <ul class="pagination">
                        <li>
                            <a href="${pageContext.request.contextPath}/user/search?currPage=${pageData.currPage==1 ? pageData.currPage : pageData.currPage - 1}&realName=${realName}" aria-label="Previous">
                                <span aria-hidden="true">&laquo;</span>
                            </a>
                        </li>
                        <c:forEach begin="${pageData.begin}" end="${pageData.end}" step="1" var="p">
                            <c:if test="${p == pageData.currPage}">
                                <li><a class="nav-active" href="${pageContext.request.contextPath}/user/search?currPage=${p}&realName=${realName}">${p}</a></li>
                            </c:if>
                            <c:if test="${p != pageData.currPage}">
                                <li><a href="${pageContext.request.contextPath}/user/search?currPage=${p}&realName=${realName}">${p}</a></li>
                            </c:if>
                        </c:forEach>
                        <li>
                            <a href="${pageContext.request.contextPath}/user/search?currPage=${pageData.currPage==pageData.totalPage ? pageData.currPage : pageData.currPage + 1}&realName=${realName}" aria-label="Next">
                                <span aria-hidden="true">&raquo;</span>
                            </a>
                        </li>
                    </ul>
                </nav>
            </div>
        </section>

        <%@include file="../assets/footer.jsp"%>
    </div>
</div>
<script>
    function addFocus(e,userId) {
        if(userId == ${loginUser.id}){
            layer.msg('不能关注自己！');
            e.preventDefault(); //取消默认事件
            return;
        }
        $.post("/user/focus",{
            "userId": userId
        }, function (flag) {
            console.log(flag);
            if(flag == 0){
                layer.msg("取关成功");
            }else{
                layer.msg("关注成功");
            }
        })
    }
</script>
</body>
</html>